package com.dong.exp_ioc;

import com.dong.exp_ioc.bean.Student;
import com.dong.exp_ioc.bean.Student2;
import com.dong.exp_ioc.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
@SpringBootApplication
public class App {

    public static void main(String[] args) {

        ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
        /*
        在src下写下面这个（类路径）
         在其他下的话 写new FileSystemXmlApplicationContext()
         */
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
        User user1 = context.getBean("user1", User.class);
        User user2 = context.getBean("user2", User.class);
        user1.add();
        System.out.println(user2);
        //add....
        //User(id=1, name=donggei)

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com/dong/exp_ioc/config");
        Student bean = applicationContext.getBean(Student.class); //根据类型 还可以根据名字
        System.out.println(bean);
        //Student(id=null, name=null)

        //2、查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
            //student2
            //studentConfig
            //getStuden
        }

        Student2 student2 = run.getBean("student2", Student2.class);
        System.out.println(student2);
        //Student2(id=111, name=dong)
    }
}